package com.yksj.healthtalk.ui.doctorstation.active;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.CommonExplainActivity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.net.http.RequestParams;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.ToastUtil;
import com.yksj.healthtalk.utils.ValidatorUtil;

/**
 * 收件信息
 * @author origin
 *
 */
public class PostAddressActivity extends BaseFragmentActivity implements OnClickListener {
	EditText nameEditText;
	EditText phoneEditText;
	EditText zipcodEditText;
	TextView addressEditText;
	JSONObject mJsonObject;
	
	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.post_address_layout);
		String content = null;
		if(bundle != null){
			content = bundle.getString("json");
		}else{
			Intent intent = getIntent();
			content = intent.getStringExtra("json");
		}
		try{
			mJsonObject = JSON.parseObject(content);
		}catch(JSONException e){
		}
		
		initUI();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString("json",mJsonObject.toJSONString());
	}
	
	@Override
	protected void onActivityResult(int request, int result, Intent data) {
		super.onActivityResult(request, result, data);
		if(result == RESULT_OK){
			addressEditText.setText(data.getStringExtra("content"));
		}
	}
	
	private void initUI(){
		initTitle();
		setBackBtn("返回",this);
		setTitle("收件信息");
		/**
		 *  Activities_id --活动ID
			Customer_id  --客户ID   
			Prize_id	--奖品ID
			Get_Prize_Time	--领取时间
			Delivery_Status --该奖品记录状态（10-未处理，未填写，20-已填写，未处理，30-已处理未发出，40-已发出，50-客户接收成功，60-客户接收失败）
			Delivery_Time       --状态时间
			Delivery_Result     --发件描述
			Addressee_Name  --收件人姓名
			Telephone   --收件人联系电话
			Zip_Code	   --收件人邮编
			Address	  --收件人详细地址                                                                                                                          
		 */
		nameEditText = (EditText)findViewById(R.id.user_name);
		phoneEditText = (EditText)findViewById(R.id.phone);
		zipcodEditText = (EditText)findViewById(R.id.post_code);
		addressEditText = (TextView)findViewById(R.id.address);
		
		nameEditText.setText(mJsonObject.getString("ADDRESSEE_NAME"));
		phoneEditText.setText(mJsonObject.getString("TELEPHONE"));
		zipcodEditText.setText(mJsonObject.getString("ZIP_CODE"));
		addressEditText.setText(mJsonObject.getString("ADDRESS"));
		
		int state = mJsonObject.getIntValue("DELIVERY_STATUS");
		if(state >= 30){
			nameEditText.setEnabled(false);
			phoneEditText.setEnabled(false);
			zipcodEditText.setEnabled(false);
			addressEditText.setEnabled(false);
		}else{
			addressEditText.setOnClickListener(this);
			setRightBtnYellowBg("确定",this);
		}
	}
	public boolean isDigit(String str) {
		boolean flag = true;
		char[] c = str.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (!Character.isDigit(c[i])) {
				flag = false;
			}
		}
		return flag;
	}
	
	private void onUpdateAddress(){
		String name = null;
		String zipCode = null;
		String phone = null;
		String address = null;
		
		if(nameEditText.length() == 0){
			nameEditText.setError("不能为空");
			return;
		}
		name = nameEditText.getText().toString();
		
		String phoneNum = phoneEditText.getEditableText().toString().trim();
		if(!ValidatorUtil.checkMobile(phoneNum)){
			phoneEditText.setError("请输入正确的手机号码");
			return;
		}
		phone = phoneEditText.getText().toString();
		
		if(zipcodEditText.length() != 6){
			zipcodEditText.setError("格式错误,长度6");
			return;
		}
		zipCode = zipcodEditText.getText().toString();
		
		if(addressEditText.length() == 0){
			addressEditText.setError("不能为空");
			return;
		}
		address = addressEditText.getText().toString();
		/**
		 * customerId //客户id
			prizeId //奖品id
			getPrizeTime //领取时间
			addresseeName //领取人姓名
			telephone //电话
			zipCode //邮编
			address //详细地址
		 **/
		RequestParams params = new RequestParams();
		params.put("customerId", mJsonObject.getString("CUSTOMER_ID"));
		params.put("prizeId", mJsonObject.getString("PRIZE_ID"));
		params.put("getPrizeTime", mJsonObject.getString("GET_PRIZE_TIME"));
		params.put("addresseeName", name);
		params.put("address", address);
		params.put("zipCode", zipCode);
		params.put("telephone", phone);
		HttpRestClient.doHttpUpdateCustomerPrizeRecord(params,new JsonsfHttpResponseHandler(this){
			
			@Override
			public void onSuccess(int statusCode, JSONObject response) {
				super.onSuccess(statusCode, response);
				if(response.containsKey("error_message")){
					String msg = response.getString("error_message");
					ToastUtil.showToastPanl(msg);
				}else{
					Intent intent = new Intent();
					intent.putExtra("json",response.toJSONString());
					setResult(RESULT_OK, intent);
					finish();
				}
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.title_right2:
			onUpdateAddress();
			break;
		case R.id.address:
			Intent intent = new Intent(this,CommonExplainActivity.class);
			intent.putExtra("title", "详细地址");  
			intent.putExtra("mContentCount", 200);  //字数限制  默认是200
			intent.putExtra("content", addressEditText.getText().toString());  //内容
			startActivityForResult(intent,100);
			break;
		}
	}
}
